package com.qf.servlet;

import com.qf.entity.Admin;
import com.qf.util.DateUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
//dadadadad dddddddddddjjjjjjj
@WebServlet(urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
//ddddddddddd
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收登录的信息
        String username =req.getParameter("username");
        String password = req.getParameter("password");
        //验证码
        String inputCode = req.getParameter("code");
        //从session在获取生成的验证码
        HttpSession session = req.getSession();
        String code =(String) session.getAttribute("code");

        long createTime =(long) session.getAttribute("code_create_time");
        if (System.currentTimeMillis() -createTime >60*1000){
            resp.sendRedirect("login.jsp?status=3");
            return;
        }

        //inputCode ->用户输入的验证码
        if (inputCode==null || !inputCode.equals(code)){
            resp.sendRedirect("login.jsp?status=2");
            return;
        }

        session.removeAttribute("code");
        session.removeAttribute("code_create_time");


        //参数的校验



        //进行登录判断
        Admin admin = DateUtils.queryAdmin(username,password);
        if (admin != null){
            //保存登录信息
            req.getSession().setAttribute("login_user",admin);

            resp.sendRedirect("index.jsp");
        }else{
            //跳转回登录页
            resp.sendRedirect("login.jsp?status=1");
        }
    }
}
